package br.com.lpoint.job.session;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceLoader;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import br.com.document.job.JobDocument;
import br.com.document.job.JobDocumentFactory;
import br.com.document.job.session.JobSession;



@Stateless
@Remote(JobSession.class)
public class JobSessionImpl implements JobSession {
	

	@Override
	public JobSession getJobSession() {
		return this;
	}

	@Override
	public List<JobDocument> getAllJobs() {
		System.out.println("Executando getAllJobs...");
		
		ServiceLoader<JobDocument> loader = null;
		loader = ServiceLoader.load(JobDocument.class);
		List<JobDocument> jobs = new ArrayList<JobDocument>();
		
		  try {
	            Iterator<JobDocument> services = loader.iterator();
	            while (services.hasNext()) {
	            	JobDocument d = services.next();
	            	jobs.add(d);
	            	System.out.println(d.getDescription());
	            }
	        } catch (Exception serviceError) {
	            serviceError.printStackTrace();
	        }
		jobs = JobDocumentFactory.createJobDocumentViewList(jobs);
		return jobs;
		
	}

}
